package com.phb.puhuibao.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.pub.dao.IBaseDao;
import com.idp.pub.service.IBaseService;
import com.idp.pub.web.controller.BaseController;
import com.phb.puhuibao.entity.MobileUser;
import com.phb.puhuibao.entity.ZhifubaoTransfer;
import com.phb.puhuibao.service.ZhifubaoTransferService;
 

@Controller
@RequestMapping(value = "/zhifubaoTransfer")
public class ZhifubaoTransferController extends BaseController<ZhifubaoTransfer, String> {
	@Resource(name = "zhifubaoTransferService")
	public void setBaseService(IBaseService<ZhifubaoTransfer, String> baseService) {
		super.setBaseService(baseService);
	}
	
	@javax.annotation.Resource(name = "zhifubaoTransferService")
	private ZhifubaoTransferService zhifubaoTransferService;
	
	@javax.annotation.Resource(name = "mobileUserDao")
	private IBaseDao<MobileUser, String> mobileUserDao;
	
	/**
	 * 新增提现
	 * @param muid
	 * @return
	 */
	@RequestMapping(value="withdrawApply")
	@ResponseBody
	public Map<String, Object> withdrawApply(@RequestParam int muid,@RequestParam String  zhifubao,@RequestParam double  amount) {
		Map<String, Object> data = new HashMap<String, Object>();
		ZhifubaoTransfer za = new ZhifubaoTransfer();
		za.setMuserId(muid);
		za.setAliAccount(zhifubao);
		za.setCreateTime(new Date());
		za.setAmount(amount);
		
        MobileUser user = mobileUserDao.get(muid+"");
		if ((user.getUserMoney() - user.getFrozenMoney()) < amount) {
			data.put("message", "用户余额不足！");
			data.put("status", 0);
			return data;
		}
		
		
		try {
			    zhifubaoTransferService.processAddNewApply(za,user,amount);
			    data.put("message", "新增成功!");
				data.put("status", 1);
				return data;
		} catch (Exception e) {
			data.put("message", "异常！");
			data.put("status", 0);
			return data;
		}
	}
	
	/**
	 * 完成提现
	 * @param muid
	 * @return
	 */
	@RequestMapping(value="finishWithdraw")
	@ResponseBody
	public Map<String, Object> finishWithdraw(@RequestParam int recordId,@RequestParam String name,@RequestParam int audituserid) {
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("recordId", recordId);
		ZhifubaoTransfer fer = this.getBaseService().unique(params);
 
		try {
			    zhifubaoTransferService.processFinnishTransfer(fer, name, audituserid);
			    data.put("message", "转账成功!");
				data.put("status", 1);
				return data;
		} catch (Exception e) {
			data.put("message", "异常！");
			data.put("status", 0);
			return data;
		}
	}

}
